﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Util
{
    /// <summary>
    /// 登录会话令牌管理
    /// </summary>
    public sealed class TokenUtil
    {
        private readonly static Dictionary<string, DateTime> tokes;

        private static TokenUtil tokenUtil;

        private static DateTime startTime = default(DateTime);

        static readonly DateTime StartDateTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local);

        private readonly static string appId = string.Empty;
        private readonly static string appsecret = string.Empty;
        private readonly static string dockinigwebUrl = string.Empty;
        private readonly static string username = string.Empty;
        private readonly static string requester = string.Empty;

        private TokenUtil() { }

        public static TokenUtil GetTokenUtil { get { return tokenUtil; } }

        static TokenUtil()
        {
            tokenUtil = new TokenUtil();
            tokes = new Dictionary<string, DateTime>();
            startTime = DateTime.Now;
        }

        private string InitToken()
        {
            Random random = new Random();
            var rIndex = random.Next(1000, 1000000);
            return rIndex + AppConfigUtil.ReadAppSettings("scode") + GetTimestamp();
        }

        /// <summary>
        /// 获取本地服务器会话令牌
        /// </summary>
        /// <returns></returns>
        public string GetToken()
        {
            string toke = string.Empty;
            var tk = this.InitToken();
            if (!tokes.ContainsKey(tk))
            {
                tokes.Add(tk, DateTime.Now);
            }
            else
            {
                return tk;
            }
            return tk;
        }

        /// <summary>
        /// 判断令牌是否存在
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public bool TokenExists(string token)
        {
            var tokentime = default(DateTime);
            tokes.TryGetValue(token, out tokentime);
            if (tokentime == default(DateTime))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 获取令牌时间
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public DateTime GetTokenTime(string token)
        {
            var tokentime = default(DateTime);
            tokes.TryGetValue(token, out tokentime);
            return tokentime;
        }

        #region 获取时间戳13位
        /// <summary>
        /// 时间戳13位
        /// </summary>
        /// <returns></returns>
        public static long GetTimestamp()
        {
            TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);//ToUniversalTime()转换为标准时区的时间,去掉的话直接就用北京时间
            return (long)ts.TotalMilliseconds; //精确到毫秒 13位
        }
        #endregion

        #region 获取时间戳10位
        /// <summary>
        /// 时间戳10位
        /// </summary>
        /// <returns></returns>
        public static long GetTimestamp10()
        {
            TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);//ToUniversalTime()转换为标准时区的时间,去掉的话直接就用北京时间
            return (long)ts.TotalSeconds;//获取10位
        }
        #endregion
    }
}
